
const TYPE_MAP = {
    food: '美食',
    view: '景点',
    hotel: '酒店',
    activity: '活动'
  }
  
  Page({
    data: {
      activeType: 'all',
      typeMap: TYPE_MAP,
      categories: [
        { type: 'all', name: '全部', count: 0 },
        { type: 'food', name: '美食', count: 0 },
        { type: 'view', name: '景点', count: 0 },
        { type: 'hotel', name: '酒店', count: 0 }
      ],
      favoritesList: []
    },
  
    onShow() {
      this.loadFavorites()
    },
  
    // 加载收藏数据
    loadFavorites() {
      const list = wx.getStorageSync('favorites') || []
      const categories = this.data.categories.map(cat => {
        const count = cat.type === 'all' 
          ? list.length 
          : list.filter(item => item.type === cat.type).length
        return { ...cat, count }
      })
      
      this.setData({
        favoritesList: list,
        categories,
        filteredList: this.filterList(list, this.data.activeType)
      })
    },
  
    // 过滤列表
    filterList(list, type) {
      return type === 'all' 
        ? list 
        : list.filter(item => item.type === type)
    },
  
    // 切换类型
    changeType(e) {
      const type = e.currentTarget.dataset.type
      this.setData({
        activeType: type,
        filteredList: this.filterList(this.data.favoritesList, type)
      })
    },
  
    // 显示删除确认
    showDeleteConfirm(e) {
      const id = e.currentTarget.dataset.id
      wx.showModal({
        title: '确认删除',
        content: '确定要取消收藏吗？',
        success: (res) => {
          if (res.confirm) {
            this.deleteFavorite(id)
          }
        }
      })
    },
  
    // 执行删除
    deleteFavorite(id) {
      const newList = this.data.favoritesList.filter(item => item.id !== id)
      wx.setStorageSync('favorites', newList)
      this.loadFavorites()
      wx.showToast({ title: '已取消收藏', icon: 'none' })
    }
  })